package com.xiaomaoguai.crs.common.websocket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;

/**
 * @ FileName: HeartBeatCheckThread.java
 * @ Author: WeiHui-Z
 * @ Date: 2017-01-06  16:04
 * @ Version: v1.0.0
 */
public class HeartBeatCheckThread implements Runnable, DisposableBean {

    private static final Logger LOGGER = LoggerFactory.getLogger(HeartBeatCheckThread.class);

    private boolean start;

    public HeartBeatCheckThread() {
        this.start = true;
    }

    @Override
    public void run() {
        while (start) {
            try {
//                Map<String, WebSocketSession> userSessions = TextMessageHandler.getUserSessions();
//                for (WebSocketSession socketSession : userSessions.values()) {
//                    heartBeatCheck(socketSession);
//                }
                Thread.sleep(10000);
            } catch (Exception e) {
                LOGGER.error("心跳检测异常", e);
            }
        }
    }

    @Override
    public void destroy() throws Exception {
        this.start = false;
    }

    /*
     * 心跳检测--保持连接存活
     *
     * @param session ws
     */
//	private void heartBeatCheck(WebSocketSession session) throws IOException {
//		WebSocketMessageModel message = new WebSocketMessageModel(CrsConstants.HEART_BEAT_CHECK);
//		message.setData(System.currentTimeMillis());
//		session.sendMessage(new TextMessage(JSON.toJSONString(message)));
//	}
}
